from pydantic import BaseModel, Field, field_validator
from enum import Enum

class UserRole(str, Enum):
    """API层角色枚举（与模型层一致）"""
    USER = "user"
    DISPATCHER = "dispatcher"
    ADMIN = "admin"

class UserRegisterRequest(BaseModel):
    """普通用户注册请求模型"""
    email: str = Field(..., example="user@example.com")
    password: str = Field(..., min_length=8, example="user123")
    password_confirm: str = Field(..., example="user123")
    full_name: str = Field(..., example="张三")
    phone: str | None = Field(None, example="13800138000")

    @field_validator("password_confirm")
    @classmethod
    def validate_passwords(cls, v: str, values) -> str:
        """验证两次输入的密码一致"""
        # 通过 values.data 获取其他字段的值
        password = values.data.get("password")
        if not password or v != password:
            raise ValueError("两次输入的密码不一致")
        return v

class AdminCreateUserRequest(BaseModel):
    """管理员创建用户请求模型"""
    email: str = Field(..., example="dispatcher@example.com")
    password: str = Field(..., min_length=8, example="dispatch123")
    full_name: str = Field(..., example="李调度员")
    phone: str | None = Field(None, example="13800138000")
    role: UserRole = Field(..., example=UserRole.DISPATCHER)  # 允许指定角色

class LoginRequest(BaseModel):
    """通用登录请求模型"""
    email: str = Field(..., example="user@example.com")
    password: str = Field(..., example="password123")

class UserResponse(BaseModel):
    """用户信息响应模型"""
    email: str
    full_name: str
    phone: str | None
    role: UserRole
    is_active: bool

    class Config:
        from_attributes = True

class TokenResponse(BaseModel):
    """登录成功响应模型"""
    access_token: str
    role: UserRole
    name: str